package com.xiangban.yanglaodajie_admin.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiangban.yanglaodajie_admin.dao.ClassifyGoodsMapper;
import com.xiangban.yanglaodajie_admin.dao.GoodsMapper;
import com.xiangban.yanglaodajie_admin.entity.ClassifyGoods;
import com.xiangban.yanglaodajie_admin.entity.Goods;
import com.xiangban.yanglaodajie_admin.entity.Ids;
import com.xiangban.yanglaodajie_admin.entity.dto.GoodsDto;
import com.xiangban.yanglaodajie_admin.entity.res.ApiResultCode;
import com.xiangban.yanglaodajie_admin.exception.BizException;
import com.xiangban.yanglaodajie_admin.utils.CommonUtils;
import com.xiangban.yanglaodajie_admin.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.xiangban.yanglaodajie_admin.utils.CommonUtils.BizEmpty;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/6/7 15:19
 */
@Service
public class ClassifyGoodsService {

    @Autowired
    private ClassifyGoodsMapper classifyGoodsMapper;
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private CommonUtils commonUtils;
    @Autowired
    private HttpServletRequest request;

    public PageInfo<ClassifyGoods> classifyGoodsList(ClassifyGoods classifyGoods){//参数：商家id
        if (commonUtils.getShopId(request) == null){
            throw new BizException(ApiResultCode.FLAG_FAIL_VOUCHER);
        }
        classifyGoods.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        PageHelper.startPage(pageUtil.getPage().getPageNum(),pageUtil.getPage().getPageSize());
        List<ClassifyGoods> list = classifyGoodsMapper.getList(classifyGoods);
        return new PageInfo<>(list);
    }

    public void addClassifyGoods(ClassifyGoods classifyGoods) {
        if (commonUtils.getShopId(request) == null){
            throw new BizException(ApiResultCode.FLAG_FAIL_VOUCHER);
        }
        classifyGoods.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        if (BizEmpty(classifyGoods.getClassifyName())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        classifyGoodsMapper.saveClassifyGoods(classifyGoods);
    }

    public void updateClassifyGoods(ClassifyGoods classifyGoods) {
        if (commonUtils.getShopId(request) == null){
            throw new BizException(ApiResultCode.FLAG_FAIL_VOUCHER);
        }
        classifyGoods.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        if (BizEmpty(classifyGoods.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(classifyGoods.getClassifyName())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(classifyGoods.getSort())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        classifyGoodsMapper.updateClassifyGoodsById(classifyGoods);
    }

    public void delClassifyGoods(Ids ids) {//参数：分类id

        if (BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        ids.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        if (BizEmpty(ids.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        Integer integer = goodsMapper.selectIsClassifyId(ids);
        Integer integer1 = goodsMapper.selectIsClassifyIds(ids);
        if (integer < 1 && integer1 < 1){
            classifyGoodsMapper.deleteClassifyGoodsById(ids);
        }else {
            throw new BizException(ApiResultCode.COMMON_MESSAGE.getCode(),"分类下有商品不可删除");
        }


    }

    public Object ClassifyGoodsInfo(ClassifyGoods classifyGoods) {
        if (BizEmpty(classifyGoods.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        return classifyGoodsMapper.getInfo(classifyGoods);
    }
}
